package cn.zyjblogs.starter.sms.autoconfigure;


import cn.zyjblogs.starter.sms.config.SmsProperties;
import cn.zyjblogs.starter.sms.provider.CompositeSmsGranter;
import cn.zyjblogs.starter.sms.provider.SmsGranter;
import cn.zyjblogs.starter.sms.provider.aliyun.AliSmsHandleGranter;
import cn.zyjblogs.starter.sms.provider.tencent.TenSmsHandleGranter;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhuyijun
 */
@Configuration
@EnableConfigurationProperties({SmsProperties.class})
public class SmsConfiguration {

    @Bean
    public SmsGranter smsGranter(SmsProperties smsProperties) {
        List<SmsGranter> list = new ArrayList<>();
        AsyncClient asyncClient = aliSyncClient(smsProperties);
        if (!Objects.isNull(asyncClient)){
            list.add(new AliSmsHandleGranter(asyncClient, smsProperties.getAliyun()));
        }
        SmsClient smsClient = tenClient(smsProperties);
        if (!Objects.isNull(smsClient)){
            list.add(new TenSmsHandleGranter(tenClient(smsProperties), smsProperties.getTencent()));
        }
        return new CompositeSmsGranter(list);
    }

    /**
     * 阿里异步客户端
     *
     * @param smsProperties
     * @return
     * @throws Exception
     */
    @Bean
    public AsyncClient aliSyncClient(SmsProperties smsProperties) {
        SmsProperties.AliyunSmsProperties aliyun = smsProperties.getAliyun();
        if (aliyun == null || Boolean.FALSE.equals(aliyun.getEnable())) {
            return null;
        }
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(aliyun.getAccessKey())
                .accessKeySecret(aliyun.getAccessSecret())
                //.securityToken("<your-token>") // use STS token
                .build());
        return AsyncClient.builder()
                .region(aliyun.getRegion())
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(aliyun.getEndpoint())
                        //.setReadTimeout(Duration.ofSeconds(30))
                )
                .build();
    }

    /**
     * 腾讯
     * @param smsProperties
     * @return
     */
    @Bean
    public SmsClient tenClient(SmsProperties smsProperties) {
        SmsProperties.TencentSmsProperties tencent = smsProperties.getTencent();
        if (tencent == null || Boolean.FALSE.equals(tencent.getEnable())) {
            return null;
        }
        com.tencentcloudapi.common.Credential credential = new com.tencentcloudapi.common.Credential(tencent.getAccessKey(), tencent.getAccessSecret());
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(tencent.getEndpoint());
        return new SmsClient(credential, tencent.getRegion());
    }
}
